package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.entity.ExPresentAudioEntity;
import com.cheer.entity.ExPresentAudioGoodEntity;
import com.cheer.vo.ExPresentAudioUserVO;
import com.google.common.collect.Lists;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户信息
 *
 * @author cheer
 */
@Mapper
@Repository
public interface ExPresentAudioDao extends BaseMapper<ExPresentAudioEntity> {

    default List<ExPresentAudioEntity> selectByPage(Integer pageNum, Integer pageSize) {
        return selectList(new QueryWrapper<ExPresentAudioEntity>()
                .orderByDesc("create_time")
                .last("limit " + (pageNum - 1) * pageSize + ", " + pageSize));
    }

    default List<ExPresentAudioEntity> selectByType(Integer pageNum, Integer pageSize, Integer type) {
        return selectList(new QueryWrapper<ExPresentAudioEntity>()
                .eq("type", type)
                .orderByDesc("vote")
                .last("limit " + (pageNum - 1) * pageSize + ", " + pageSize));
    }

    default ExPresentAudioEntity selectByLimit(Integer number) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>()
                .orderByDesc("create_time")
                .last("limit " + number + ", 1"));
    }

    default ExPresentAudioGoodEntity selectByRandom(Integer random) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>()
                .last("limit " + random + ", 1"));
    }

    default ExPresentAudioEntity selectByTypeLimit(Integer number, Integer type) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>()
                .eq("type", type)
                .orderByDesc("vote")
                .last("limit " + number + ", 1"));
    }

    default ExPresentAudioEntity selectByTypeRandom(Integer random, Integer type) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>()
                .eq("type", type)
                .last("limit " + random + ", 1"));
    }

    default List<ExPresentAudioEntity> selectByUserId(Integer userId) {
        return selectList(new QueryWrapper<ExPresentAudioEntity>()
                .eq("user_id", userId)
                .orderByDesc("create_time"));
    }

    default ExPresentAudioEntity selectOneByUserId(Integer userId) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>()
                .eq("user_id", userId).last("limit 1"));
    }


    Integer countByPath(String path);

    Integer countByPathTotal(String path);

    List<ExPresentAudioUserVO> selectByUser(@Param("path") String path, @Param("name") String name);

    default int deleteByUserId(Integer id) {
        return delete(new QueryWrapper<ExPresentAudioEntity>().eq("user_id", id));
    }

    Integer sumOfVote(Integer id);

    default IPage<ExPresentAudioEntity> selectPageByUserId(Integer pageNum, Integer pageSize, Integer id) {
        return selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<ExPresentAudioEntity>()
                .eq("user_id", id).orderByDesc("create_time"));
    }

    void incrVote(Integer id);

    void decrVote(Integer id);

    default ExPresentAudioEntity selectNext(Integer id, Integer userId) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>()
                .eq("user_id", userId).lt("id", id).orderByDesc("id").last("limit 1"));
    }

    default ExPresentAudioEntity selectPrev(Integer id, Integer userId) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>()
                .eq("user_id", userId).gt("id", id).orderByAsc("id").last("limit 1"));
    }

    default IPage<ExPresentAudioEntity> selectPageByStatus(Integer pageNum, Integer pageSize, String path) {
        return selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<ExPresentAudioEntity>()
                .eq("status", 1)
                .likeRight("path", path));
    }

    default ExPresentAudioEntity selectNextUnreview(Integer id) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>().eq("status", 1)
               .gt("id", id).orderByAsc("id").last("limit 1"));
    }

    default ExPresentAudioEntity selectPrevUnreview(Integer id) {
        return selectOne(new QueryWrapper<ExPresentAudioEntity>().eq("status", 1)
                .lt("id", id).orderByDesc("id").last("limit 1"));
    }

    Integer countByPathUnreview(String path);

    default void updateByUserId(ExPresentAudioEntity entity) {
        update(entity, new QueryWrapper<ExPresentAudioEntity>().eq("user_id", entity.getUserId()));
    }
}
